layui.config({
    base: '/js/layui-expand/' //插件路径
}).extend({
    enhanceform: 'enhanceform'
});

layui.use(['table', 'element','layer','laytpl','form','enhanceform','laypage','laydate'], function(){
    var $ = layui.jquery, layer = layui.layer, table = layui.table, form = layui.form,laydate = layui.laydate;

    var options = {
        elem: '#endtime'
        ,type: 'datetime'
        ,value: new Date()
        ,format: 'yyyy-MM-dd HH:mm'
    }

    laydate.render(options);

    //监听开关
    var myInterval ;
    form.on('switch(switchFresh)', function(data){
        var switchFlag = data.elem.checked;
        if(switchFlag){
            var frequency = $('#frequency').val()
            myInterval = setInterval(function(){
                reloadTable("",frequency);
            }, frequency*60*1000);
            $('#search_div').attr("style","display:none;");
            $('#search_button').attr("style","display:none;");
            $('#search_text').attr("style","display:none;");
        }else{
            clearInterval(myInterval)
            $('#search_div').attr("style","display:block;");
            $('#search_button').attr("style","display:block;");
            $('#search_text').attr("style","display:block;");
        }
    });

    var datatable = table.render({
        elem: '#pulldata_table_id'
        ,id: 'pulldatatable_id'
        ,url:'/list'
        ,cellMinWidth: 100
        ,page: {
            limit:10,
            limits:[10,20,30,40,50]
        }

        ,cols: [
            [
                {field:'id',  title: 'ID', sort: true,hide:true}
                ,{field:'uploadtime',  title: '时间',hide:true}
                ,{field:'eqaddress',  width:200,title: '定位设备', sort: true}
                ,{field:'eqno',  width:200,title: '设备号', sort: true}
                ,{field:'studentcount',  title: '人数'}
                ,{field:'action', width:120, title: '操作',toolbar: '#bar',fixed: 'right'}
            ]
        ]
    });


    function reloadTable(endtime,frequency) {
        table.reload('pulldatatable_id', {
            where: {
                endtime: endtime
                ,frequency: frequency
            }
            ,page: {
                curr: 1
            }
        });
    }

    $('.layui-btn-search').on('click', function(){
        var endtime = $('#endtime').val()
        datatable.reload({
            where: {
                endtime: endtime
            }
            ,page: {
                curr: 1
            }
        });
    });


    table.on('tool(pulldata_table_filter)', function(obj){
        var data = obj.data ;
        layer.open({
            type: 1,
            title: '详情',
            area: ['700px', '550px'],
            fixed: false, //不固定
            maxmin: false,
            content: '<div><table id="templateTable"></table></div>',
            success: function(layero, index) {
                loadDetailTable(data.eqno,data.uploadtime)
            }
        });
    });

    function loadDetailTable(eqno,uploadtime) {
        var frequency = $('#frequency').val()
        table.render({
            elem: '#templateTable'
            ,id: 'detailtable_id'
            ,url:'/detail'
            ,where: {
                eqno:eqno,
                endtime:uploadtime,
                frequency:frequency
            }
            ,cellMinWidth: 100
            ,page: {
                limit:10,
                limits:[10,20,30,40,50]
            }
            ,cols: [
                [
                    {field:'id',  title: 'ID', sort: true,hide:true}
                    ,{field:'name',  width:200,title: '姓名', sort: true}
                    ,{field:'lno',  width:200,title: '绑定标签号', sort: true}
                    ,{field:'sex',  title: '性别'}
                    ,{field:'age',  title: '年龄'}
                ]
            ]
        });
    }

});
